package com.yima.baseproj.module.sys.student;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yima.baseproj.aop.log.Logging;
import com.yima.baseproj.module.config.utils.PageUtil;
import com.yima.baseproj.module.config.utils.PageVo;
import com.yima.baseproj.module.config.utils.ResultUtil;
import com.yima.baseproj.module.config.utils.ResultVo;
import com.yima.baseproj.module.sys.clazz.ClassService;
import com.yima.baseproj.module.sys.kindergarten.KindergartenEntity;
import com.yima.baseproj.module.sys.kindergarten.KindergartenService;
import com.yima.baseproj.module.sys.student.model.StudentExcel;
import com.yima.baseproj.module.sys.student.model.StudentSelectModel;
import com.yima.baseproj.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Api(tags = "学生管理")
@RequestMapping("/api/sys/student")
@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;
    @Autowired
    private ClassService classService;

    @Logging(tags = "学生管理",value = "查询分页")
    @PreAuthorize("hasAuthority('sys:student:findAllByPage')")
    @ApiOperation("查询分页")
    @GetMapping("/findAllByPage")
    public Object findAllByPage(PageVo page, StudentEntity searchModel) {
        Page<Map> pageData = studentService.findAllByPage(PageUtil.initMpPage(page),searchModel);
        return pageData;
    }

    @PreAuthorize("hasAuthority('sys:student:findAllByList')")
    @ApiOperation("查询列表")
    @GetMapping("/findAllByList")
    public ResultVo getStudent(StudentEntity student) {
        List<Map> studentList = studentService.findAllByList(student);
        return ResultUtil.success(studentList);
    }

    @ApiOperation("根据班级id 查询学生列表")
    @GetMapping("/findAllByClassId/{classId}")
    public ResultVo findAllByClassId(@PathVariable int classId) {
        List<StudentSelectModel> studentList = studentService.findAllByClassId(classId);
        return ResultUtil.success(studentList);
    }

    @ApiOperation("根据学生姓名查询学生列表")
    @GetMapping("/findAllByStuName")
    public ResultVo findAllByClassId(String studentName) {
        List<StudentSelectModel> studentList = studentService.sysStudentFindBystuName(studentName);
        return ResultUtil.success(studentList);
    }


    @Logging(tags = "学生管理",value = "添加")
    @PreAuthorize("hasAuthority('sys:student:add')")
    @ApiOperation("添加")
    @PostMapping("/add")
    public void saveUser(@RequestBody @Validated StudentEntity student) {

        studentService.add(student);
    }

    @ApiOperation("根据id查询学生信息")
    @GetMapping("/findById/{id}")
    public StudentEntity findById(@PathVariable int id){
        StudentEntity studentEntity = studentService.findById(id);
        if(studentEntity.getClassId()!=null){
            int classId = studentEntity.getClassId();
            int kgId = classService.findById(classId).getKgId();
            int kgClass[] =new int[2];
            kgClass[0]=kgId;
            kgClass[1]=classId;
            studentEntity.setKgClass(kgClass);
        }
        return studentEntity;
    }

    @Logging(tags = "学生管理",value = "修改")
    @PreAuthorize("hasAuthority('sys:student:edit')")
    @ApiOperation("修改")
    @PostMapping("/edit")
    public void editKg(@RequestBody @Validated StudentEntity customAdmin) {
        studentService.edit(customAdmin);
    }

    @Logging(tags = "学生管理",value = "删除")
    @PreAuthorize("hasAuthority('sys:student:delByIds')")
    @ApiOperation("删除")
    @DeleteMapping(value = "/delByIds/{ids}")
    public void delByIds(@PathVariable int ids) {

        studentService.delByIds(ids);
    }


    /**
     * 下载Excel模板
     */
//    @SecurityIgnoreUrl(isNeedResultVo = false)
    @RequestMapping("/excel/template")
    public void downloadTemplate(HttpServletResponse response) {
        String fileName = "导出模板";
        String sheetName = "导出学生模板";
        List<StudentExcel> sutdents = new ArrayList<>();
        sutdents.add(new StudentExcel("xx幼儿园","xx班","","1/2","2008-08-08"));
        try {
            ExcelUtil.writeExcel(response, sutdents, fileName, sheetName, StudentExcel.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PostMapping("/import")
    public void ImportExcel(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        if (multipartFile.isEmpty()) {
            return;
        }
        studentService.importStudent(multipartFile);

    }


}
